package com.ljh.mall.order.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ljh.common.exception.NoStockExecption;
import com.ljh.mall.order.service.OrderService;
import com.ljh.mall.order.vo.OrderConfirmVo;
import com.ljh.mall.order.vo.OrderResponseVO;
import com.ljh.mall.order.vo.OrderSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/toTrade")
    public String toTrade(Model model){
        // TODO 查询订单确认页需要的信息
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        System.out.println(JSON.toJSONString(confirmVo));
        model.addAttribute("confirmVo",confirmVo);
        return "confirm";
    }

    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO vo, Model model, RedirectAttributes redirectAttributes){
        Integer code = 0;
        OrderResponseVO responseVO = null;
        try{
            responseVO = orderService.submitOrder(vo);
            code = responseVO.getCode();
        }catch (NoStockExecption e){
            code = 2;
        }

        if(code == 0){
            model.addAttribute("orderResponseVO",responseVO);
            // 表示下单操作成功
            return "pay";
        }else{
            System.out.println("code=" + code);
            String msg = "订单失败";
            if(code == 1){
                msg = msg + ":重复提交";
            }else if(code == 2){
                msg = msg + ":锁定库存失败";
            }
            //redirectAttributes.addAttribute("msg",msg);
            System.out.println("msg"+msg);
            redirectAttributes.addFlashAttribute("msg",msg);
            // 表示下单操作失败
            return "redirect:http://localhost:8030/toTrade";
        }
    }
}
